package cn.exempale.mscapplication.binding;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import cn.exempale.mscapplication.R;
import cn.exempale.mscapplication.emnus.ProgressType;

import androidx.databinding.BindingAdapter;

/**
 * 自定义xml app：属性
 * visibleGone<>通用属性，用于view的显示和隐藏</>
 * imageUrl<>ImageView属性，用于glide加载图片url</>
 */
public class BindingAdapters {
    /**
     * view 显示或者隐藏
     *
     * @param view
     * @param <>true</>显示
     */
    @BindingAdapter("visibleGone")
    public static void showHide(View view, boolean show) {
        view.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    /**
     * 处理结果状态文字颜色
     *
     * @param view
     * @param progressType {@link ProgressType}
     */
    @SuppressLint("ResourceAsColor")
    @BindingAdapter("progressTypeCode")
    public static void dynamicTextColor(View view, int progressType) {
        TextView textView = (TextView) view;
        if (progressType == ProgressType.TRANSFER_UPLOAD.getCode())
            textView.setTextColor(R.color.teal_200);
        else if (progressType > 0)
            textView.setTextColor(R.color.color_ff333333);
        else
            textView.setTextColor(R.color.color_fff50057);
    }

    /**
     * 结果显示右侧多行样式
     *
     * @param view
     * @param length
     */
    @BindingAdapter("multiTextAlignment")
    public static void multiRightTextStyle(View view, int length) {
        TextView textView = (TextView) view;
        int len = textView.getLineCount();
        if (len == 1)
            textView.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
        else
            textView.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
    }
}
